/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UI_Clientes.java
 *
 * Created on May 3, 2010, 1:34:19 PM
 */

package UI;

import Excepciones.DinamicaDato;
import Excepciones.ProcesamientoDato;
import Excepciones.Sistema;
import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException;
import java.sql.SQLException;
import javax.swing.JTable;
import Modelo.BaseDato;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import robot_maker.Cliente;
import robot_maker.Manager;
import robot_maker.ResultSetTableModel;

/**
 *
 * @author CarlosScipione
 */
public class UI_Clientes extends javax.swing.JFrame {


    private int rowMyor = 1;

    /** Creates new form UI_Clientes */
    public UI_Clientes() {
        initComponents();
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

     private void llenar (JTable tabla, String query){
            try{
            try {
                BaseDato buscar = new BaseDato();
            } catch (CommunicationsException ex) {
                Sistema.loggear(10, "LLENAR",
                        "COMUNICACION PERDIDA CON LA BASE DE DATOS",
                        UI_Clientes.class, "CommunicationsException");
            } catch (DinamicaDato ex) {
            }
            tabla.setModel(new ResultSetTableModel(query));
            if (tabla.getRowCount() > 0) {
                tabla.setRowSelectionInterval(0, 0);
            }
        } catch (ClassNotFoundException ex) {
                        Sistema.loggear(10, "LLENAR",
                        "COMUNICACION PERDIDA CON LA BASE DE DATOS",
                        UI_Clientes.class, "CommunicationsException");
        } catch (SQLException ex) {
                        Sistema.loggear(11, "LLENAR",
                        "EN EL SQL DE EJECUCCION",
                        UI_Clientes.class, "SQLException");
        }
    }
    public void refrescar (){
         llenar(cliente_table, "select cirif \"CI\", nombre \"Nombre\", " +
                 "apellido \"Apellido\", estado \"Estado\" from CLIENTE");

    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        cliente_table = new javax.swing.JTable();
        delete_boton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Clientes");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jButton1.setText("+");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Clientes:");

        cliente_table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        cliente_table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cliente_tableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(cliente_table);

        delete_boton.setText("-");
        delete_boton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delete_botonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(delete_boton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1))
                .add(23, 23, 23))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jLabel1)
                        .add(432, 432, 432))
                    .add(layout.createSequentialGroup()
                        .add(24, 24, 24)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 386, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButton1)
                            .add(delete_boton))))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        addCliente window = new addCliente(this, false, -1);
        window = (addCliente) Manager.centra(window);
        window.setVisible(true);
}//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
      llenar(cliente_table, "select cirif \"CI\", nombre \"Nombre\", " +
              "apellido \"Apellido\", estado \"Estado\" from CLIENTE");
    }//GEN-LAST:event_formWindowOpened

    private void cliente_tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cliente_tableMouseClicked
         rowMyor = cliente_table.getSelectedRow() + 1;
        if(evt.getClickCount() == 2){
              ResultSet lista = null;
            try {
                lista = BaseDato.ejecutarSqlSelect("select ciRif from CLIENTE");
            } catch (DinamicaDato ex) {
            }
            try {
                int i =1;
                lista.next();
                while(i < rowMyor){
                    lista.next();
                    i++;
                }
            } catch (SQLException ex) {
                        Sistema.loggear(11, "MouseClicked",
                        "EN EL SQL DE EJECUCCION",
                        UI_Clientes.class, "SQLException");            }

        addCliente window = null;
            try {
                window = new addCliente(this, true, lista.getInt(1));
            } catch (SQLException ex) {
                    Sistema.loggear(11, "MouseClicked",
                        "EN EL SQL DE EJECUCION",
                        UI_Clientes.class, "SQLException");
            }
        window = (addCliente) Manager.centra(window);
        window.setVisible(true);
        }

    }//GEN-LAST:event_cliente_tableMouseClicked

    private void delete_botonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delete_botonActionPerformed
        Cliente eliminar = new Cliente();
        ResultSet lista = null;
        try {
            lista = BaseDato.ejecutarSqlSelect("select ciRif from CLIENTE");
        } catch (DinamicaDato ex) {
        }
            try {
                int i =1;
                lista.next();
                while(i < rowMyor){
                    lista.next();
                    i++;
                }
            try {
                 Object[] options = {"Si",
                    "No",
                    "Cancelar"};
        int n = JOptionPane.showOptionDialog(null,
            "Esto borrara toda la informacion del usuario incluyendo " +
            "compras de robots",
            "¿Esta seguro?",
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            options[2]);
                eliminar.borrarBD(lista.getInt(1), n);
            } catch (ProcesamientoDato ex) {
            }
            } catch (SQLException ex) {
                        Sistema.loggear(11, "DELETEBOTON",
                        "EN EL SQL DE EJECUCION",
                        UI_Clientes.class, "SQLException");             }
         refrescar();
    }//GEN-LAST:event_delete_botonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UI_Clientes().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable cliente_table;
    private javax.swing.JButton delete_boton;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
